// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.web.browser;

import java.io.File;

import org.dengues.ui.i18n.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Tree;

/**
 * @author yyang
 */
public class ImageCapture {

    /**
     * Return the image of the argument. This includes the client and non-client area, but does not include any child
     * controls. To get child control use {@link ImageCapture#getImage(Control, int, int, boolean)}.
     * 
     * @param aControl
     * @param maxWidth
     * @param maxHeight
     * @return image or <code>null</code> if not valid for some reason. (Like not yet sized).
     * @since 1.1.0
     */
    public static Image getImage(Control aControl) {
        Rectangle rect = aControl.getBounds();
        if (rect.width <= 0 || rect.height <= 0)
            return null;
        Image image = new Image(aControl.getDisplay(), rect.width, rect.height);
        int print_bits = OS.PRF_NONCLIENT | OS.PRF_CLIENT | OS.PRF_ERASEBKGND;
        // This method does not print immediate children because the z-order
        // doesn't work correctly and needs to be
        // dealt with separately, however Table's TableColumn widgets are
        // children so must be handled differently
        boolean specialClass = aControl instanceof Table || aControl instanceof Browser || aControl instanceof OleFrame
                || aControl instanceof CCombo;
        try {
            specialClass |= aControl instanceof Spinner;
        } catch (NoClassDefFoundError e) {
        } // might not be on 3.1 of SWT
        if (specialClass) {
            print_bits |= OS.PRF_CHILDREN;
        }
        GC gc = new GC(image);
        // Need to handle cases where the GC font isn't automatically set by the
        // control's image (e.g. CLabel)
        // see bug 98830 (https://bugs.eclipse.org/bugs/show_bug.cgi?id=98830)
        Font f = aControl.getFont();
        if (f != null)
            gc.setFont(f);
        int hwnd = aControl.handle;
        if (aControl instanceof Tree) {
            int hwndParent = OS.GetParent(hwnd);
            if (hwndParent != aControl.getParent().handle) {
                hwnd = hwndParent;
                print_bits |= OS.PRF_CHILDREN;
            }
        }
        OS.SendMessage(hwnd, OS.WM_PRINT, gc.handle, print_bits);
        gc.dispose();
        return image;
    }

    /**
     * Check if the image is a black one.
     * 
     * @param image
     * @return
     */
    public static boolean isImageEmpty(Image image) {
        if (image != null) {
            ImageData imageData = image.getImageData();
            if (imageData == null) {
                return true;
            }
            int x = imageData.width / 2;
            int height = imageData.height;
            for (int y = 0; y < height; y++) {
                int pixel = imageData.getPixel(x, y);
                if (pixel != -256) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Save Images by a image name.
     * 
     * @param image
     * @param name
     * @deprecated
     */
    public static void saveImage(Image image, String name) {
        ImageLoader imageLoader = new ImageLoader();
        File imageDir = new File("E:\\Images"); //$NON-NLS-1$
        if (imageDir.exists()) {
            imageDir.delete();
        }
        imageDir.mkdir();
        ImageData imageData = image.getImageData();
        if (imageData == null)
            return;
        imageLoader.data = new ImageData[] { imageData };
        File file = new File(imageDir.getAbsolutePath() + "\\" + name + ".jpg"); //$NON-NLS-1$ //$NON-NLS-2$
        imageLoader.save(file.getAbsolutePath(), SWT.IMAGE_JPEG);
    }
}
